package com.villaday.idf.custom;

import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.villaday.idf.R;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.ToastUtil;


/**
 * 加载等待对话框
 */
public class LoadingSaveDialog extends DialogFragment {

    private ImageView ivLoading;
    private TextView tvLoading;

    public static LoadingSaveDialog newInstance(String str) {
        LoadingSaveDialog loadingDialog = new LoadingSaveDialog();
        Bundle bundle = new Bundle();
        bundle.putString("str", str);
        loadingDialog.setArguments(bundle);
        return loadingDialog;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_save_loading, container);
        ivLoading = (ImageView) view.findViewById(R.id.iv_loading);
        tvLoading = (TextView) view.findViewById(R.id.tv_loading);
        if (TextUtils.isEmpty(getArguments().getString("str"))) {
            tvLoading.setVisibility(View.GONE);
        } else {
            tvLoading.setVisibility(View.VISIBLE);

        }
        RotateAnimation rotate = (RotateAnimation) AnimationUtils.loadAnimation(getActivity(), R.anim.rotate);
        ivLoading.startAnimation(rotate);
        tvLoading.setText(getArguments().getString("str"));


        //点击对话框外不可取消
        getDialog().setCanceledOnTouchOutside(false);
        //设置对话框背景
        getDialog().getWindow().setBackgroundDrawableResource(R.drawable.shape_solid_trans_gray);
        //取消标题显示
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        return view;
    }

    public void setLoadingTips(String msg) {
        if (tvLoading != null) {
            tvLoading.setText(msg);
        }

    }


}
